import type { App, PropType, Plugin } from 'vue-demi'

export type Data = Record<string, unknown>

type DefaultFactory<T> = (props: Data) => T | null | undefined

export interface PropOptions<T = any, D = T> {
  type?: PropType<T> | true | null
  required?: boolean
  default?: D | DefaultFactory<D> | null | undefined | Data

  validator?(value: unknown): boolean
}

export const withInstallObject = <T, K extends any[]>({
  parent,
  children,
}: {
  parent: T
  children: K
}) => {
  const c = parent as any
  const cList = children as any[]
  c.install = function (app: App) {
    app.component(c.displayName || c.name, parent)
    cList.forEach((item) => {
      app.component(item.displayName || item.name, item)
    })
  }
  return parent as T & Plugin
}

export const withInstall = <T, K>(comp: T, ...compList: K[]) =>
  withInstallObject({ parent: comp, children: compList })
